package tz.postprocessing;

import java.util.HashMap;

public class AnalysedResult {

	//ime clustra c testnih podatkih
	public String clusterTest;
	//stevilo clankov v tem clustru
	public int numOfCasesInCluster;
	//mapa in primerjanje z vsemi osalimi clustri iz rezultata
	HashMap<String, Integer> clusterMatches;
	//razred iz rezultata, ki ima najvec ujemanj
	public String clusterResultMost;
	//stevilo teh ujemanj (st clankov v tem clustru) največ st ujemanj
	public int clusterResultnum = 0;
	public int clusterTestSize = 0;
	double entropy = 0;
	
	public AnalysedResult(String clusterTest, int numOfCasesInCluster){
		this.clusterTest = clusterTest;
		this.numOfCasesInCluster = numOfCasesInCluster;
		this.clusterResultMost = null;
		this.clusterResultnum = 0;
		this.clusterTestSize= 0;
		clusterMatches = new HashMap<String, Integer>();

	}
	
	public void addClusterResult(String clusterResult, int value, int clusterTestSize){
		clusterMatches.put(clusterResult, value);
		
		if(value >= this.clusterResultnum){
		
			this.clusterResultnum = value;
			this.clusterResultMost = clusterResult;
			this.clusterTestSize = clusterTestSize;
		}
		
		//za racunanje entropije
		double x = (double)((double)value / (double)clusterTestSize);
		if(x!=0){
			double current = x*log2x(x);
			this.entropy = this.entropy - current;
			
		}
	
	}
	
	String getClustersAndMatches(){
		
		String ret = clusterTest+": "+this.numOfCasesInCluster;
		ret = ret + " mathcing Cluster: "+this.clusterResultMost+" num: "+this.clusterResultnum+" size: "+clusterTestSize+"\n";
		
		for(String topic : clusterMatches.keySet()){
			
			Integer numOfMatches = clusterMatches.get(topic);
			ret = ret + topic+": "+numOfMatches+" ";			
		}
		ret = ret+"\n";
		
		
		return ret;
	}
	
	
	String getClustersAndMostMatches(){
		String ret = clusterTest+": "+this.numOfCasesInCluster+"\n";
		ret = ret + " mathcing Cluster: "+this.clusterResultMost+" num: "+this.clusterResultnum+" size: "+clusterTestSize+"\n\n";
		return ret;
	}
	
	String getClusterMatchesAndMostMathes(){
		String ret = clusterTest+"\t"+this.numOfCasesInCluster+"\t"+this.clusterResultMost+"\t"+this.clusterResultnum+"\t"+clusterTestSize+"\n";
		
		return ret;
	}
	
	public AnalysedResult clone(){
		AnalysedResult clone = new AnalysedResult(this.clusterTest, this.numOfCasesInCluster);
		clone.clusterMatches = this.clusterMatches;
		clone.clusterResultMost = this.clusterResultMost;
		clone.clusterResultnum = this.clusterResultnum;
		clone.clusterTestSize = this.clusterTestSize;
		clone.entropy = this.entropy;
		return clone;
	}
	
	public static double log2x(double x){
		
		return (double)((double)Math.log(x)/(double)Math.log(2));
	}

	
}
